package cn.qqy.org.controller;

import cn.qqy.base.annotation.RonghuanetPermission;
import cn.qqy.org.domain.Department;
import cn.qqy.org.domain.Employee;
import cn.qqy.org.service.IDepartmentService;
import cn.qqy.org.utils.AjaxResult;
import cn.qqy.org.utils.DepartmentQuery;
import cn.qqy.org.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description = "部门相关功能")
@RonghuanetPermission(name = "部门")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

//    @RequestMapping("/or")
    @ApiOperation(value = "添加或修改")
    @PutMapping//只接受put情求//支持PutMapping("/or")
    @RonghuanetPermission(name = "部门添加或修改")
    public AjaxResult asveOrUpdate(@ApiParam(value = "id为空是增,反之为该") @RequestBody Department department){
        try {
            System.out.println(department);
            if (department.getId()==null) {
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务异常"+e.getMessage());
        }
    }

//    @RequestMapping("/delete")
    @DeleteMapping("/{id}")
    @RonghuanetPermission(name = "部门删除(id)")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务异常"+e.getMessage());
        }
    }

//    @RequestMapping("/findall")
    @GetMapping
    @RonghuanetPermission(name = "部门查询全部")
    public List<Department> findall( ){
        try {
            List<Department> findall = departmentService.findall();
//            System.out.println(findall);
            return findall;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    @RequestMapping("/findid")
    @GetMapping("/{id}")
    @RonghuanetPermission(name = "部门查询(id)")
    public Department findid(@PathVariable("id") Long id ){
        try {
            return departmentService.findid(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @PostMapping
    public PageList<Department> find1(@RequestBody DepartmentQuery departmentQuery ){
//        System.out.println(departmentQuery.getstartIndex()+"  123213");
        try {
//
            return departmentService.findid1(departmentQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/findmanager")
    public List<Employee> findmanager(){
        try {

            return departmentService.findmanager();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/tree")
    public List<Department> findtree(){
        try {

            return departmentService.findtree();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {

            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败："+e.getMessage());
        }

    }

}
